package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAccountTrace;
import com.qf.service.AccountTraceService;
import com.qf.service.UserService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/brokerage")
public class BrokerageController {

    @Autowired
    private AccountTraceService accountTraceService;
    @Autowired
    private UserService userService;


    //http://localhost:8080/admin/brokerage/list?
    // page=1&limit=20&username=lkh&mobile=321&statusArray=0&statusArray=1
    // &statusArray=2&sort=add_time&order=desc
    @RequiresPermissions(value = "admin:brokerage:list")
    @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String username,String mobile,String sort,String order,
                       Integer[] statusArray){
        boolean flag=false;
        if ((username!=null&&username.trim().length()>0)||(mobile!=null&&mobile.trim().length()>0)){
            flag=true;
        }

        List<Integer> uIds = userService.findIdByUsernameAndMobile(username,mobile);
        if (flag&&(uIds==null||uIds.size()==0)){
            uIds=new ArrayList<>();
            uIds.add(-1);
        }
        IPage<DtsAccountTrace> iPage = accountTraceService.findListByQuery(page,limit,sort,order,statusArray,uIds);
        Map data=new HashMap();
        data.put("total",iPage.getTotal());
        data.put("traceList",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    //id:14
    //status:1/2
    //traceMsg:"asdf"
    @RequiresPermissions(value = "admin:brokerage:approve")
    @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button =  "审批")
    @PostMapping("/approve")
    public Object approve(@RequestBody String json){
        Integer id = JacksonUtil.parseInteger(json, "id");
        Integer status = JacksonUtil.parseInteger(json, "status");
        String traceMsg = JacksonUtil.parseString(json, "traceMsg");

        accountTraceService.updateStatus(id,status,traceMsg);
        return ResponseUtil.ok();
    }


}
